{# Renders field for bootstrap 3 standards.

    Params:
        field - WTForm field
        kwargs - pass any arguments you want in order to put them into the html attributes.
        There are few exceptions: for - for_, class - class_, class__ - class_

    Example usage:
        {{ macros.render_field(form.email, placeholder='Input email', type='email') }}
#}
{% macro render_field(field, label_visible=true) -%}
    {{ "<tr>"|safe if 'o' in kwargs.get('tr','oc')  }}
    <td class="{% if field.errors %}has-error{% endif %} {{ kwargs.pop('class_', '') }}  ">
        {% if (field.type != 'HiddenField' and field.type !='CSRFTokenField') and label_visible %}
            <label for="{{ field.id }}" class="control-label">{{ field.label }}  {% if field.flags.required %} <span style="font-weight: bold;color: red;">*</span> {% endif %} </label>
        {% endif %}
    </td><td {{ "colspan="+(kwargs.pop('colspan')|string) if kwargs.get('colspan') }} >
{#        {{ field(class_='form-control', **kwargs) }}#}
    {{ field(**kwargs) }}
        {% if field.errors %}
            {% for e in field.errors %}
                <p class="help-block">{{ e }}</p>
            {% endfor %}
        {% endif %}
    </td>    {{ "</tr>"|safe if 'c' in kwargs.get('tr','oc') }}
{%- endmacro %}

{# Renders checkbox fields since they are represented differently in bootstrap
    Params:
        field - WTForm field (there are no check, but you should put here only BooleanField.
        kwargs - pass any arguments you want in order to put them into the html attributes.
        There are few exceptions: for - for_, class - class_, class__ - class_

    Example usage:
        {{ macros.render_checkbox_field(form.remember_me) }}
 #}
{% macro render_checkbox_field(field) -%}
    {{ "<tr>"|safe if 'o' in kwargs.get('tr','oc')  }}
        <td>
            {{ field.label }}
        </td>
        <td>
        {# field(type='checkbox', **kwargs) #}
           <input type="checkbox" name="{{ field.id }}" id="{{ field.id }}" value="y" {{ 'checked' if field.object_data  }}>{{ label }}
        </td>
    {{ "</tr>"|safe if 'c' in kwargs.get('tr','oc') }}
{%- endmacro %}

{# Renders radio field
    Params:
        field - WTForm field (there are no check, but you should put here only BooleanField.
        kwargs - pass any arguments you want in order to put them into the html attributes.
        There are few exceptions: for - for_, class - class_, class__ - class_

    Example usage:
        {{ macros.render_radio_field(form.answers) }}
 #}
{% macro render_radio_field(field) -%}
    {% for value, label, _ in field.iter_choices() %}
        <div class="radio">
            <label>
                <input type="radio" name="{{ field.id }}" id="{{ field.id }}" value="{{ value }}">{{ label }}
            </label>
        </div>
    {% endfor %}
{%- endmacro %}

{# Renders WTForm in bootstrap way. There are two ways to call function:
     - as macros: it will render all field forms using cycle to iterate over them
     - as call: it will insert form fields as you specify:
     e.g. {% call macros.render_form(form, action_url=url_for('login_view'), action_text='Login',
                                        class_='login-form') %}
                {{ macros.render_field(form.email, placeholder='Input email', type='email') }}
                {{ macros.render_field(form.password, placeholder='Input password', type='password') }}
                {{ macros.render_checkbox_field(form.remember_me, type='checkbox') }}
            {% endcall %}

     Params:
        form - WTForm class
        action_url - url where to submit this form
        action_text - text of submit button
        class_ - sets a class for form
    #}
{% macro render_form(form, action_url='', action_text='Submit', class_='', btn_class='btn btn-default') -%}

    <form method="POST" action="{{ action_url }}" role="form" class="{{ class_ }}">
        {{ form.hidden_tag() if form.hidden_tag }}
        {% if caller %}
            {{ caller() }}
        {% else %}
            {% for f in form %}
                {% if f.type == 'BooleanField' %}
                    {{ render_checkbox_field(f) }}
                {% elif f.type == 'RadioField' %}
                    {{ render_radio_field(f) }}
                {% else %}
                    {{ render_field(f) }}
                {% endif %}
            {% endfor %}
        {% endif %}
        <button type="submit" class="{{ btn_class }}">{{ action_text }} </button>
    </form>
{%- endmacro %}


<style>
.edittab {margin: 3px;border: solid black 1px;width: 99%}
.edittab td:nth-child(2n+1) {max-width: 200px;padding-right: 15px;padding-left:5px;}
label {font-size: 12px; font-weight: normal;}
</style>
<form method="POST" role="form" class="">
<table class="edittab">
  <tr><td colspan="4"><b>颗粒物模块信息 </b></td></tr>
{{ render_field(form.pprojid,style="width:50px;",tr='o') }}
{{ render_field(form.ctd_origfilename,tr='') }}
{{ render_field(form.qualitytaxo,tr='') }}
{{ render_field(form.daterecalculhistotaxo,tr='c') }}

{{ render_field(form.profileid,tr='o') }}
{{ render_field(form.ctd_import_name,tr='') }}
{{ render_field(form.qualitypart,tr='') }}
{{ render_checkbox_field(form.histobrutavailable,tr='c') }}

{{ render_field(form.sampleid,tr='o') }}
{{ render_field(form.ctd_import_email,tr='') }}
{{ render_field(form.ctd_status,tr='c') }}

{{ render_field(form.filename,tr='o') }}
{{ render_field(form.ctd_import_datetime,tr='') }}
{{ render_field(form.ctd_desc,tr='c',style="width:500px;",colspan=3) }}

</table>
<table class="edittab">
  <tr><td colspan="4"><b>Profile METADATA</b></td></tr>
{{ render_field(form.instrumsn,tr='o') }}
{{ render_field(form.stationid,tr='') }}
{{ render_field(form.acq_barcode,tr='') }}
{{ render_field(form.winddir,tr='c') }}

  {{ render_field(form.sampledate,tr='o') }}
{{ render_field(form.latitude,tr='') }}
{{ render_field(form.firstimage,tr='') }}
{{ render_field(form.winspeed,tr='c') }}


{{ render_field(form.op_sample_name,tr='o') }}
{{ render_field(form.longitude,tr='') }}
{{ render_field(form.lastimg,tr='') }}
{{ render_field(form.seastate,tr='c') }}

{{ render_field(form.op_sample_email,tr='o') }}
{{ render_field(form.bottomdepth,tr='') }}
{{ render_field(form.lastimgused,tr='') }}
{{ render_field(form.nebuloussness,tr='c') }}

  <tr><td></td><td></td><td></td><td></td>
{{ render_field(form.imp_descent_filtered_row,tr='') }}
{{ render_field(form.imp_removed_empty_slice,tr='c') }}

{{ render_checkbox_field(form.yoyo,tr='o') }}
{{ render_checkbox_field(form.organizedbydeepth,tr='') }}
{{ render_field(form.comment,tr='c',style="width:550px;",colspan=7) }}
</table>
<table class="edittab">
<tr><td colspan="4"><b>UVP Settings</b></td></tr>
{{ render_field(form.acq_aa,tr='o',style="width:80px;") }}
<td></td><td></td>
{{ render_field(form.acq_ratio,tr='',style="width:80px;") }}
{{ render_field(form.proc_datetime,tr='c') }}

{{ render_field(form.acq_exp,tr='o',style="width:80px;") }}
{{ render_field(form.acq_shutterspeed,tr='',style="width:80px;") }}
{{ render_field(form.acq_xsize,tr='',style="width:80px;") }}
{{ render_field(form.proc_gamma,tr='c',style="width:80px;") }}

{{ render_field(form.acq_volimage,tr='o',style="width:80px;") }}
{{ render_field(form.acq_exposure,tr='',style="width:80px;") }}
{{ render_field(form.acq_ysize,tr='',style="width:80px;") }}
{{ render_field(form.proc_soft,tr='c') }}

{{ render_field(form.acq_pixel,tr='o',style="width:80px;") }}
{{ render_field(form.acq_gain,tr='',style="width:80px;") }}
{{ render_field(form.acq_eraseborder,tr='',style="width:80px;") }}
{{ render_field(form.proc_process_ratio,tr='c',style="width:80px;") }}


{{ render_field(form.acq_depthoffset,tr='o',style="width:80px;") }}
{{ render_field(form.acq_filedescription,tr='') }}
{{ render_field(form.acq_threshold,tr='c',style="width:80px;") }}



{{ render_field(form.acq_smbase,tr='o',style="width:80px;") }}
{{ render_field(form.acq_tasktype,tr='') }}
<td colspan="2"></td>


{{ render_field(form.acq_smzoo,tr='o',style="width:80px;") }}
{{ render_field(form.acq_choice,tr='c') }}

  {{ render_field(form.acq_presure_gain,tr='o',style="width:80px;") }}
{{ render_field(form.acq_disktype,tr='') }}
{{ render_field(form.acq_descent_filter,tr='c') }}
</table>
<table class="edittab">
  <tr><td colspan="4"><b>LISST Settings</b></td></tr>
{{ render_field(form.lisst_zscat_filename,tr='o') }}
{{ render_field(form.lisst_kernel,tr='c') }}
</table><table>
    <tr>
        <td colspan="3">
<button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-save "></span> Save</button>
<a href="/part/prj/{{ prjid }}" class="btn btn-warning"> Cancel</a>
{% if psampleid>0 %}
    <button type="button" onclick="$('#confirm').modal('show')" class="btn btn-danger"><span class="glyphicon glyphicon-erase"></span> Remove Sample</button>
    <input type="checkbox" name="forcerecalc" id="forcerecalc" value="Y"> Force Histogram computation when saving
{% endif %}

        </td>
    </tr>
</table>
</form>
<script>
  $('#acq_aa,#acq_exp,#acq_volimage,#acq_pixel,#profileid').change(function () {
    $('#forcerecalc').prop('checked',true);
  });
</script>


<div class="modal " id="confirm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="ModalLabel">Are you sure you want remove this sample</h4>
      </div>
      <div class="modal-body">
            <form method="post" action="/part/sampleedit/{{ psampleid }}" style="display: inline-block">
        <input type="hidden" name="delete" value="Y">
    <button  class="btn btn-danger"><span class="glyphicon glyphicon-erase"></span> Confirm Remove sample</button>
        </form>
    <button  type="button" class="btn btn-warning" onclick="$('#confirm').modal('hide')"> Cancel</button>

      </div>
    </div>
  </div>
</div>
